import { syncSetTimeout, sendMessage, getMessage, dispatch } from "../../global/tools.js";

export default class CallHistory {
    static count = 0;
    constructor(webController) {
        CallHistory.count++;
        if (CallHistory.count >= 2) {
            alert("collectionPlugin: \nError: CallHistory.count >= 2，即将刷新页面。");
            location.reload(true);
        } 
        this.webController = webController;
        this.config = {
            history: []
        };
        // 监听事件
        {
            this.eventListenersFn = {
                click: {
                    title: "click",
                    fn: async e => {
                        function getInfo(keyName) {
                            return jQuery(`div#detail div.caseinfo ul.case-item > li > div.key:contains('${keyName}')`).next("div.value").text().trim()
                        }
                        if (e.target.innerText == "提交催记") {
                            let caseInfo = {
                                caseId: window.location.href.split("/").pop(),
                                "案件编号": getInfo("案件编号"),
                                "商户号": getInfo("商户号"),
                            };
                            for (let i = 0; i < 8; i++) {
                                await syncSetTimeout(100);
                                if (jQuery("span:contains('操作成功！')").length > 0) {
                                    this.config.history.push(caseInfo);
                                    break;
                                }
                            }
                        }
                    }
                }
            };
            // 开始监听相关事件
            for (const key in this.eventListenersFn) {
                this.webController.document().addEventListener(this.eventListenersFn[key].title, this.eventListenersFn[key].fn, this.eventListenersFn[key].capture ? true : false);
            }
        }
        this.init();
    }
    init() {
    }
}